package com.foreknow.controller;

import com.foreknow.model.Admin;
import com.foreknow.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //非表单查询
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        表单post
//     response   resp存值  request取值req

        //用来设置服务器端 向客户端返回的内容类型 以及字符编码集
//        resp.setContentType("text/html;charset = utf-8");
//        //客户端向服务器端请求内容的字符编码集
//        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        //如何获取到请求的参数 username = tom  & password = 123456
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminServiceImpl service = new AdminServiceImpl();
        Admin admin = service.isValidate(username,password);
        if(admin!=null){
            //将用户登录的名字保存到服务器端
            session.setAttribute("admin",admin);
           // req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
            resp.sendRedirect(req.getContextPath()+"/user/manager?q=list");
        }else {
            out.println("登录失败");
        }

    }
}




































